package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.other.FileDto;
import com.dg.cuckoo.admin.dto.proCarryOut.ProChangeDto;
import com.dg.cuckoo.admin.repository.FileRepository;
import com.dg.cuckoo.admin.repository.ProChangeRepository;
import com.dg.cuckoo.admin.service.FileService;
import com.dg.cuckoo.admin.service.OneChangeService;
import com.dg.cuckoo.admin.service.ProjectInfoServer;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class OneChangeServiceimpl implements OneChangeService {

    @Autowired
    private ProChangeRepository repository;
    @Autowired
    private ProjectInfoServer projectInfoServer;
    @Autowired
    private FileRepository fileRepository;
    @Autowired
    private FileService fileService;

    @Override
    public void save(String projectId, ProChangeDto dto, MultipartFile[] files) throws Exception {
        checkSave(dto);
        dto.setProjectId(projectId);
        ProChangeDto save = repository.save(dto);
        if(files!=null){
            projectInfoServer.powerfulFileSave(files,save.getId());
        }
    }

    @Override
    public void delect(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        repository.deleteById(id);
        List<FileDto> byByMark = fileRepository.findByByMark(id);
        fileRepository.deleteAll(byByMark);
    }

    @Override
    public String openInfo(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        ProChangeDto dto = repository.findById(id).get();
        List<String> files = fileService.byByMark(id);
        JSONObject json = new JSONObject();
        json.put("dto",dto);        //项目合同表信息
        json.put("files",files);    //文件urls
        return json.toString();
    }

    @Override
    public void update(String id, String projectId, MultipartFile[] files, ProChangeDto dto) throws Exception {
        if(id==null){
            throw new ServiceException("id主键");
        }
        checkSave(dto);
        dto.setId(id);
        ProChangeDto save = repository.save(dto);
        fileRepository.deleteAll(fileRepository.findByByMark(id));
        if(files.length>0){
            projectInfoServer.powerfulFileSave(files,save.getId());
        }
    }

    @Override
    public PageModel page(Integer page, Integer pageSize, String projectId, Integer changeType) {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        if(changeType==null){
            throw new ServiceException("请选择项目类别");
        }
        Integer one = page;
        page = (page - 1) * pageSize;
        int size = repository.findByProjectIdAndChangeType(projectId, changeType).size();
        List<ProChangeDto> dtos = repository.page(projectId, changeType, page, pageSize);
        PageModel model = new PageModel(one, pageSize, size, dtos);
        return model;
    }

    public void checkSave(ProChangeDto dto){
        if (dto.getChangeType()==null){
            throw new ServiceException("请选择变更类别");
        }
    }
}
